package com.frog.framework.dynamo;

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.enhanced.dynamodb.Key;
import software.amazon.awssdk.enhanced.dynamodb.TableSchema;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;

import java.net.URI;

/**
 * @author wuqiong 2022/7/21
 * @description
 */
public class TestDynamoDb {


    public static void main(String[] args) {
        DynamoDbClient client = getClient();
        DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder().dynamoDbClient(client).build();
        getTableItem(enhancedClient);
    }

    public static void getTableItem(DynamoDbEnhancedClient enhancedClient) {

        // user_dynamic_message 为 dynamoDB的表名
        DynamoDbTable<UserDynamicMessage_Test> dynamicMessage =
                enhancedClient.table("user_dynamic_message", TableSchema.fromBean(UserDynamicMessage_Test.class));

        // 170873 为 数据主键
        Key key = Key.builder().partitionValue("86724026").build();
        // dynamicMessage.deleteItem(key);
        // 获取主键所对应的数据
        UserDynamicMessage_Test messageItem = dynamicMessage.getItem(key);


//        messageItem.setUserDynamicMessagesJSON("[{\"createTimeStamp\":\"1658490481942\",\"friendUserId\":\"86724026\",\"type\":\"video\",\"videoId\":\"68388347\"},{\"createTimeStamp\":\"1658490481942\",\"friendUserId\":\"86724026\",\"type\":\"IM\",\"videoId\":\"68388347\"}]");


//        // 更新
//        dynamicMessage.updateItem(messageItem);
        DynamoDbTable<UserDynamicMessage_Test> dynamicMessageBest =
                enhancedClient.table("user_dynamic_message_best", TableSchema.fromBean(UserDynamicMessage_Test.class));
        UserDynamicMessage_Test messageItem2 = dynamicMessageBest.getItem(key);
        // dynamicMessageBest.deleteItem(key);



        System.out.println(messageItem2 == null);
//        System.out.println(messageItem.getType());

    }


    public static DynamoDbClient getClient() {
        return DynamoDbClient.builder()
                .endpointOverride(URI.create("https://dynamodb.us-east-2.amazonaws.com"))
                .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("AKIARBDMLH525WYKYAXY", "W5CGQBwxtXCdbp+e2nojIq/2nYk/iuu/Rh7VMjl6")))
                .region(Region.US_EAST_2)
                .build();
    }

}
